<template>
  <el-dialog :visible.sync="visible" width="55%">
    <el-table :data="rpData" style="width: 100%">
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="名称"
        prop="name"
      >
      </el-table-column>
      <el-table-column align="center" label="数据来源" prop="pf"> </el-table-column>
      <el-table-column align="center" label="售卖价格" prop="totalPrice"> </el-table-column>
      <el-table-column align="center" label="优惠价格" prop="reducedPrice"> </el-table-column>
      <el-table-column align="center" label="客户支付" prop="payPrice"> </el-table-column>
      <el-table-column align="center" label="去佣金价" prop="buyFxFz"> </el-table-column>
      <el-table-column align="center" label="分销价格" prop="buyPrice"> </el-table-column>
      <el-table-column align="center" label="分销支付" prop="buyFxMt"> </el-table-column>
      <el-table-column align="center" label="利润" prop="profit"> </el-table-column>
      <el-table-column align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="handleRecive(scope.$index, scope.row)"
            >下单</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="button">
      <el-col :span="4">
        <el-input
          size="small"
          v-model="reciveOtherData.price"
          placeholder="采购总价/分"
          clearable
        ></el-input>
      </el-col>

      <el-select v-model="state" size="small" placeholder="线下人工采购渠道">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <el-col :span="8">
        <el-input
          size="small"
          v-model="reciveOtherData.orderCode"
          placeholder="采购订单号"
          clearable
        ></el-input>
      </el-col>

      <el-button size="small" type="success" @click="handleReciveOther()">发货</el-button>
    </div>

    <div class="button">
      <el-button size="mini" type="danger" @click="handleClose()">关单</el-button>
      <el-button size="mini" type="danger" @click="handleRefuse()">确认无房</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "1",
          label: "携程",
        },
        {
          value: "2",
          label: "美团",
        },
        {
          value: "3",
          label: "其他",
        },
      ],
      state: "",
      multipleSelection: [],
      rpData: [],
      //value: "1",
      visible: false,
      dataForm: {
        id: 0,
      },
      reciveOtherData: {
        price: null,
        orderCode: null,
      },
      rules: {
        price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
        orderCode: [{ required: true, message: "订单号不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        //this.$refs["rpData"].resetFields();
        if (this.dataForm.id) {
          console.info(123);
          this.$http({
            url: this.$http.adornUrl("/ord/order/rateslist"),
            method: "get",
            params: this.$http.adornParams({
              id: this.dataForm.id,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.rpData = data.list;
              console.info(data)
            }
          });
        }
      });
    },
    // 日期拼接
    date(data) {
      return data.inDate + "~" + data.outDate;
    },
    // 预定总价
    inAllPrice(data) {
      let allPrice = 0;
      let rate = data.rates;
      for (var value of rate) {
        allPrice += value.sellPrice * value.num;
      }
      return allPrice + "(分)";
    },
    // 采购总价计算
    buyAllPrice(data) {
      let allPrice = 0;
      let rate = data.rates;
      for (var value of rate) {
        allPrice += value.buyPrice * value.num;
      }
      return allPrice;
    },
    // 利润
    getPrice(data) {
      let buyPrice = data.payPrice;
      let sellPrice = 0;
      let rate = data.rates;
      for (var value of rate) {
        //buyPrice += value.buyPrice * value.num;
        sellPrice += value.sellPrice * value.num;
      }
      return buyPrice - sellPrice;
    },
    // 下单
    handleRecive(index, row) {
      this.$confirm(`确定下单操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/ord/order/buy"),
            method: "get",
            params: this.$http.adornParams({
              id: this.dataForm.id,
              rpCode: row.rpCode,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
              });
            } else {
              this.$message.error(data.msg);
            }
            this.visible = false;
          });
        })
        .catch(() => {
          console.log("取消操作");
        });
    },
    // 自采下单
    handleReciveOther(index, row) {
      this.$confirm(`确定下单操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/ord/order/buyother"),
          method: "get",
          params: this.$http.adornParams({
            id: this.dataForm.id,
            otherOrderCode: this.reciveOtherData.orderCode,
            price: this.reciveOtherData.price,
            optType: 2,
            status: this.state,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1000,
            });
          } else {
            this.$message.error(data.msg);
          }
          this.reciveOtherData.price = "";
          this.reciveOtherData.orderCode = "";
          this.visible = false;
        });
      });
      // .catch(() => {
      //   console.log("取消操作");
      // });
    },
    // 关单
    handleClose(index, row) {
      this.$confirm(`确定拒单操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/ord/order/refuse"),
            method: "get",
            params: this.$http.adornParams({
              id: this.dataForm.id,
              optType: 6,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
              });
            } else {
              this.$message.error(data.msg);
            }
            this.visible = false;
          });
        })
        .catch(() => {
          console.log("取消操作");
        });
    },
    // 确认无房
    handleRefuse(index, row) {
      this.$confirm(`确定拒单操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/ord/order/refuse"),
            method: "get",
            params: this.$http.adornParams({
              id: this.dataForm.id,
              optType: 1,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
              });
            } else {
              this.$message.error(data.msg);
            }
            this.visible = false;
          });
        })
        .catch(() => {
          console.log("取消操作");
        });
    },
  },
};
</script>
<style>
.button {
  margin-top: 25px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
